<?php
class ControllerModuleFullProductPath extends Controller {
	private $error = array(); 
	
	public function index() {   
	
		$this->load->language('module/full_product_path');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('setting/setting');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
			$this->model_setting_setting->editSetting('full_product_path', $this->request->post);		
			$this->session->data['success'] = $this->language->get('text_success');
			//$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else $this->data['success'] = '';
		
		if (isset($this->session->data['error'])) {
			$this->data['error'] = $this->session->data['error'];
			unset($this->session->data['error']);
		} else $this->data['error'] = '';
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_module'] = $this->language->get('button_add_module');
		$this->data['button_remove'] = $this->language->get('button_remove');
		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_module'),
			'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('module/full_product_path', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('module/full_product_path', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');
		
		if (isset($this->request->post['full_product_path_largest'])) {
			$this->data['full_product_path_largest'] = (isset($this->request->post['full_product_path_largest'])) ? true : false;
		}elseif ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->data['full_product_path_largest'] = false;
		} else {
			$this->data['full_product_path_largest'] = $this->config->get('full_product_path_largest');
		}
    
    if (isset($this->request->post['full_product_path_bypasscat'])) {
			$this->data['full_product_path_bypasscat'] = (isset($this->request->post['full_product_path_bypasscat'])) ? true : false;
		}elseif ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->data['full_product_path_bypasscat'] = false;
		} else {
			$this->data['full_product_path_bypasscat'] = $this->config->get('full_product_path_bypasscat');
		}
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['full_product_path_categories'])) {
			$this->data['full_product_path_categories'] = $this->request->post['full_product_path_categories'];
		}elseif ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->data['full_product_path_categories'] = array();
		} else {
			$this->data['full_product_path_categories'] = $this->config->get('full_product_path_categories') ? $this->config->get('full_product_path_categories') : array();
		}		
		
		// categories management
		$this->load->model('catalog/category');
		$this->data['categories'] = $this->model_catalog_category->getCategories(0);
		
		$this->template = 'module/full_product_path.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	public function install() {
		$this->load->model('setting/setting');
		$this->model_setting_setting->editSetting('full_product_path', array('full_product_path_largest' => true));
		if(is_file(DIR_CATALOG.'../vqmod/xml/full_product_path.xml.disabled'))
			rename(DIR_CATALOG.'../vqmod/xml/full_product_path.xml.disabled', DIR_CATALOG.'../vqmod/xml/full_product_path.xml');
	}
	
	public function uninstall() {
		if(is_file(DIR_CATALOG.'../vqmod/xml/full_product_path.xml'))
			rename(DIR_CATALOG.'../vqmod/xml/full_product_path.xml', DIR_CATALOG.'../vqmod/xml/full_product_path.xml.disabled');
	}
	
	private function validate() {
		if (!$this->user->hasPermission('modify', 'module/full_product_path')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		if (!$this->error)
			return true;
		return false;
	}
}
?>